package org.example.cdpcrpbackend.util;

import cn.hutool.crypto.SecureUtil;

public class CodeTool {

    /**
     * 密钥secret + 活动id ==> MD5 ==> e10adc3949ba59abbe56e057f20f883e
     * @ + 活动ID   ==> @1
     * code: e10adc3949ba59abbe56e057f20f883e@1
     */
    private final static String SECRET = "CDP_SECRET";

    public static String createCode(String uid) {
        String md5 = SecureUtil.md5(SECRET + uid);
        return md5 + "@" + uid;
    }

    public static boolean verify(String code) {
        String uid = code.split("@")[1];
        String md5 = SecureUtil.md5(SECRET + uid);
        return code.startsWith(md5);
    }

    public static String getUid(String code) {
        return code.split("@")[1];
    }
}
